package com.yanqu.road.server.cmd.activity.tonbingchannel;


import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tonbingchannel.ChannelTonBingModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;

import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_CTON_BING_EXCHANGE_SKIN, desc = "统兵演武兑换服装")
public class TonBingExchangeSkinCmd extends AbstractCommand {

    private int skinId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChannelTonBingProto.CTonBingExchangeSkinReqMsg reqMsg = ChannelTonBingProto.CTonBingExchangeSkinReqMsg.parseFrom(packet.getBytes());
        skinId = reqMsg.getSkinId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChannelTonBingConfig config = ChannelTonBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        ret = player.getModule(ChannelTonBingModule.class).exchangeSkin(skinId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ChannelTonBingProto.CTonBingExchangeSkinRespMsg.Builder respMsg = ChannelTonBingProto.CTonBingExchangeSkinRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_CTON_BING_EXCHANGE_SKIN, respMsg);
        }
    }
}
